require(['./config'], () => {
    require(['jquery', 'artTemplate', 'loadHeaderFooter'], ($, template) => {
        //点击导航栏跳转到商品分类页面
        $('.sort').on('click', e => {
            location.href = '/html/sort.html'
            return false
        })

        // 点击更多跳转到商品分类页面
        $('.p-more').on('click', e => {
            location.href = '/html/sort.html'
            return false
        })

        //点击购物车跳转至购物车页面
        $('header').on('click', '.top-car', () => {
            location.href = '/html/cart.html'
        })

        //加载推荐商品数据
        $.ajax({
            url: 'http://www.xiongmaoyouxuan.com/api/tab/1/feeds?start=80&sort=0',
            method: 'GET',
            data: {
                start: 80,
                sort: 0
            }
        }).done((resData) => {
            const html = template('tuijian-template', { product: resData.data.list })
            $('.product').html(html)
        })

        // 加载有品秒杀数据
        $.ajax({
            url: 'http://www.xiongmaoyouxuan.com/api/tab/1/feeds?start=160&sort=0',
            method: 'GET',
            data: {
                start: 160,
                sort: 0
            }
        }).done((resData) => {
            const html = template('swiper1', { product: resData.data.list })
            $('.pro-item').html(html)
        })

        // 加载每日新品数据
        $.ajax({
            url: 'http://www.xiongmaoyouxuan.com/api/tab/1/feeds?start=200&sort=0',
            method: 'GET',
            data: {
                start: 200,
                sort: 0
            }
        }).done((resData) => {
            const html = template('swiper2', { product: resData.data.list })
            $('.new-item').html(html)
        })

        // 为每个商品绑定点击事件，跳转到详情页
        // 利用编程式导航来实现页面跳转
        $('.product').on('click', '.prod', e => {
            const id = $(e.currentTarget).data('id')
            location.href = '/html/detail.html?id=' + id
        })
        $('body').on('click', '.slide-item', e => {
            const id = $(e.currentTarget).data('id')
            location.href = '/html/detail.html?id=' + id
        })

        // 秒杀倒计时
        const date1 = new Date('2021-06-11 20:00:00')
        const daojishi = () => {
            const date2 = new Date()
            // 获取时间差
            const sjc = (date1.getTime() - date2.getTime()) / 1000
            // 小时
            let hours = parseInt(sjc / 60 / 60)
            hours = hours < 10 ? '0' + hours : hours
            // 分钟 
            let minute = parseInt(sjc / 60 % 60)
            minute = minute < 10 ? '0' + minute : minute
            // 秒
            let second = parseInt(sjc % 60)
            second = second < 10 ? '0' + second : second
            $('.hour').text(hours)
            $('.minute').text(minute)
            $('.second').text(second)
        }
        setInterval(daojishi ,1000)


        /************* 点击登陆弹出协议框及页面跳转 ****************/
        // 绑定点击事件
        $('body').on('click', '.log-in , .agree , .n-agree , .close', e => {
            if ($(e.currentTarget).is('.log-in')) {// 登陆弹出协议
                $('.login').css({'display':"block"})
                return false
            }else if ($(e.currentTarget).is('.agree')) {// 同意并跳转
                location.href = '/html/login.html'
                $('.login').css({'display':"none"})
            } else {// 不同意或者关闭协议框
                location.reload()
                $('.login').css({'display':"none"})
            }
        })

        /************* 点击注册弹出协议框及页面跳转 ****************/
        // 绑定点击事件
        $('body').on('click', '.sign-up', () => {
            location.href = '/html/sign-up.html'
        })
            


    })
})